<template>
  <div class="dashboard-container">
    <el-table :data="tableData" border>
      <el-table-column prop="id" label="ID" align="center" width="150">
      </el-table-column>
      <!-- <el-table-column prop="name" label="名称" align="center">
      </el-table-column> -->
      <el-table-column prop="title" label="标题" align="center">
      </el-table-column>
      <el-table-column prop="num" label="发布数量" align="center">
      </el-table-column>
    </el-table>
    <Pagination2
      :currentPage="this.form.pageNo"
      :size="this.form.pageSize"
      :total="total"
      @changelimit="changelimit"
      @changecurrentPage="changecurrentPage"
    ></Pagination2>
  </div>
</template>

<script>
import {
  posttypes,
} from "@/api/servetypeIndex";
export default {
  name: "Dashboard",
  components: {
  },
  data() {
    return {
      that: this,
      reqCategory1: [],
      reqCategory2: [
        { id: 0, name: "全部" },
        { id: 1, name: "未推送" },
        { id: 2, name: "已推送" },
      ],
      form: {
        name: "",
        phone: "",
        pageNo: 1,
        pageSize: 15,
        adminid:Number(localStorage.getItem('userId'))
      },
      reqCategoryLoaded: false, // 新增标志
      total: 0,
      tableData: [],
    };
  },
  // beforeCreate() {
  //   that = this;
  // },
  filters: {
    classifyidfilter(val, that) {
      if (that.reqCategoryLoaded) {
        // 检查标志
        const matchingCategory = that.reqCategory1.find(
          (item) => item.id === val
        );
        return matchingCategory ? matchingCategory.title : "";
      }
      return "";
    },
    statefilter(val) {
      if (val == 1) {
        return "未推送";
      } else {
        return "已推送";
      }
    },
    datefilter(val) {
      if (val) {
        var date = new Date(parseInt(val) * 1000);
        var Year = date.getFullYear();
        var Moth =
          date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1;
        var Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var Hour =
          date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var Minute =
          date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var Sechond =
          date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var GMT = Year + "-" + Moth + "-" + Day;
        // "   " +
        // Hour +
        // ":" +
        // Minute +
        // ":" +
        // Sechond;

        return GMT;
      } else {
        return "";
      }
    },
  },
  methods: {
    searchclick() {
      this.getcouponlist1();
    },
    resetSearch() {
      this.form.name = "";
      this.form.phone = "";
      this.getcouponlist1();
    },
    // 子传父:自定义事件
    refresh() {
      this.getcouponlist1();
    },
    async gettypelist() {
      let result = await gettypelist({adminid:Number(localStorage.getItem('userId'))});
      console.log(result);
      if (result.code == 0) {
        this.reqCategory1 = result.data;
        this.reqCategoryLoaded = true; // 设置标志为 true
      }
    },
    async getcouponlist1() {
      let result = await posttypes(this.form);
      console.log(result);
      if (result.code == 0) {
        if (result.data.data != []) {
          this.tableData = result.data.data;
          this.total = result.data.totalCount;
          this.tableData.forEach((item, index) => {
            // if (item.iamges != "") {
            //   this.$set(item, "images", item.images.split(","));
            // }
          });
          console.log("最后", this.tableData);
        }
      }
    },
    changelimit(val) {
      this.form.pageSize = val;
      this.getcouponlist1();
    },
    changecurrentPage(val) {
      this.form.pageNo = val;
      this.getcouponlist1();
    },
    changerenyuan(val) {
      this.$refs.add.edit(val);
    },
  },
  watch: {
    "form.name": {
      handler(newVal) {
        if (newVal == "") {
          this.getcouponlist1();
        }
      },
    },
  },
  mounted() {
    this.getcouponlist1();
    this.gettypelist();
  },
};
</script>

<style lang="scss" scoped>
// .dashboard {
//   &-container {
//     margin: 30px;
//   }
//   &-text {
//     font-size: 30px;
//     line-height: 46px;
//   }
// }
</style>
